//# sourceURL=http://uMapApp/Module/Bridge/BridgeDetectionModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/PanelBlock/ChartBlock"
    , "../../UI/Panel/PanelBlock/TableBlock"
    , "../../UI/Panel/PanelBlock/TreeBlock"
    , "../../UI/Tree/Tree"
    , "../../UI/Panel/Item/ListItem"
    , "./BridgeDetection"
    , "./BridgeDetectionNodeBlock"
    , "../../UI/Checkbox"
    , "../../UI/Panel/Panel"
], function (
    mhjs
    , uMap
    , uEarth
    , Module
    , FloatPanel
    , ListBlock
    , PropertiesBlock
    , ChartBlock
    , TableBlock
    , TreeBlock
    , Tree
    , ListItem
    , BridgeDetection
    , BridgeDetectionNodeBlock
    , Checkbox
    , Panel
) {

    function BridgeDetectionModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "桥梁位移";
        _this.icon = _this.app.icons.bridge;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showMinMax": true, "title": "桥梁列表"});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = "桥梁位移监测点";
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        _this.chartPanel = new FloatPanel({
            "class": "leftPanel BridgeDetectionModule chartPanel", "showMinMax": true, "title": "检测数据图表"
        });
        _this.chartPanel.chartBlock = new ChartBlock({
            "title": _this.title,
            "chart": {
                "height"     : 270,
                "width"      : 740,
                "chartOption": {
                    xAxis  : {
                        type       : "category",
                        boundaryGap: false
                    },
                    yAxis  : {
                        type: "value"
                    },
                    series : {
                        type: "line"
                    },
                    grid   : {
                        top         : "0%",
                        left        : "0%",
                        right       : "0%",
                        bottom      : "0%",
                        containLabel: true
                    },
                    tooltip: {
                        trigger        : "item",
                        backgroundColor: "rgb(1, 20, 30)",
                        borderColor    : "#333",
                        textStyle      : {
                            color: "#fff"
                        },
                        confine        : true
                    }
                }
            }
        });

        _this.chartPanel.tableBlock = new TableBlock();

        _this.chartPanel.tableBlock.head = [
            {
                "label" : "#",
                "col"   : {"width": 50},
                "format": function (tableBlock, td) {
                    td.text(tableBlock.body.length + 1);
                }
            },
            {
                "value": "data",
                "label": "数值"
            },
            {
                "value": "time",
                "label": "更新时间"
            }
        ];

        _this.chartPanel.tableBlock.ui.title.hide();
        _this.chartPanel.tableBlock.ui.filterBlock.hide();

        _this.chartPanel
            .append(_this.chartPanel.chartBlock)
            .append(_this.chartPanel.tableBlock)
        ;

        _this.chartPanel.on("close", function () {
            if (_this.chartPanel.bridgeDetectionNode) {
                _this.chartPanel.bridgeDetectionNode.bridgeDetection.unActiveAllNode();
            }
        });

        _this.panels.push(_this.chartPanel);


        _this.nodeListPanel = new FloatPanel({"class": "rightPanel BridgeDetectionModule", "showClose": true});
        _this.nodeListPanel.treeBlock = new TreeBlock({"class": "fullBlock"});

        _this.nodeListPanel.treeBlock.tree.status = _this.nodeListPanel.treeBlock.tree.add(new Tree({
            "title": "状态",
            "list" : []
        }));
        _this.nodeListPanel.treeBlock.tree.types = _this.nodeListPanel.treeBlock.tree.add(new Tree({
            "title": "类型",
            "list" : []
        }));

        _this.nodeListPanel.treeBlock.title = "桥梁位移监测点";
        _this.nodeListPanel.append(_this.nodeListPanel.treeBlock);

        _this.nodeListPanel.on("close", function () {
            if (_this.nodeListPanel.bridgeDetection) {
                _this.closeNode(_this.nodeListPanel.bridgeDetection);
            }
        });

        _this.panels.push(_this.nodeListPanel);

        _this.totalPanel = new Panel({"class": "scModule totalPanel noStyle"});

        _this.totalPanel.detectionCount = _this.renderShCountBlockItem({
            "title": "监测桥梁数量",
            "count": 2,
            "unit" : "座"
        });

        _this.totalPanel.detectionNodeCount = _this.renderShCountBlockItem({
            "title": "传感器数量",
            "count": 10,
            "unit" : "个"
        });


        _this.totalPanel
            .append(_this.totalPanel.detectionCount)
            .append(_this.totalPanel.detectionNodeCount)
        ;

        _this.panels.push(_this.totalPanel);

        _this.app.ui.layout.top.append(_this.totalPanel);
        _this.app.ui.layout.right.append(_this.listPanel);
        _this.app.ui.layout.right.append(_this.nodeListPanel);
        _this.app.ui.layout.left.append(_this.chartPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);
    }

    var _class = BridgeDetectionModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"                  : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"            : {
            value: function (list) {

                var _this = this;

                _this.bridges = {};

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;

                    item.nodeRequest = _this.node.request;
                    item.nodeDataRequest = _this.node.dataRequest;
                    item.nodeMap = _this.node.map;
                    item.module = _this;

                    var bridgeDetection = new BridgeDetection(item);

                    _this.items.push(bridgeDetection);

                    _this.bridges[bridgeDetection["编号"]] = bridgeDetection;

                    bridgeDetection.listItem = new ListItem({
                        "title"          : bridgeDetection["名称"],
                        "subtitle"       : bridgeDetection["类型"],
                        "icon"           : _this.icon.base64,
                        "bridgeDetection": bridgeDetection
                    });


                    bridgeDetection.marker.on("click", function () {
                        _this.openNode(bridgeDetection);
                    });

                    bridgeDetection.listItem.on("click", function () {
                        _this.openNode(bridgeDetection);
                    });

                    bridgeDetection.listItem.on("hide", function (listItem) {
                        listItem.bridge.close();
                    });

                    bridgeDetection.listItem.on("show", function (listItem) {
                        listItem.bridge.open();
                    });

                    _this.listPanel.listBlock.add(bridgeDetection.listItem);

                });

                _this.listPanel.listBlock.filter = {
                    "param": "bridgeDetection.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "bridgeDetection.sortKey",
                        "label": "桩号"
                    }
                };
            }
        },
        "openNode"              : {
            value: function (bridgeDetection) {

                var _this = this;

                _this.isOpened && _this.totalPanel.close(false);

                $.each(_this.items, function (i, item) {
                    item !== bridgeDetection && item.close();
                });

                bridgeDetection.layer.active();

                bridgeDetection.openNode(function () {

                    _this.nodeListPanel.treeBlock.tree.status.clear();
                    _this.nodeListPanel.treeBlock.tree.types.clear();

                    _this.nodeListPanel.treeBlock.tree.status.classNames = {};
                    _this.nodeListPanel.treeBlock.tree.types.classNames = {};

                    $.each(bridgeDetection.nodeDic.status, function (i, statusType) {

                        _this.nodeListPanel.treeBlock.tree.status.classNames[statusType.className] = _this.nodeListPanel.treeBlock.tree.status.add(new Tree({
                            "title": statusType.label,
                            "list" : [],
                            "class": statusType.className
                        }));
                    });

                    _this.nodeListPanel.treeBlock.tree.status.title = "状态 ( " + _this.nodeListPanel.treeBlock.tree.status.list.length + " )";
                    _this.nodeListPanel.treeBlock.tree.status.openChild();


                    $.each(bridgeDetection.nodeDic.type, function (i, typeType) {

                        _this.nodeListPanel.treeBlock.tree.types.classNames[typeType.className] = _this.nodeListPanel.treeBlock.tree.types.add(new Tree({
                            "title": typeType.label,
                            "list" : [],
                            "class": typeType.className
                        }));

                    });

                    _this.nodeListPanel.treeBlock.tree.types.title = "类型 ( " + _this.nodeListPanel.treeBlock.tree.types.list.length + " )";
                    _this.nodeListPanel.treeBlock.tree.types.openChild();

                    $.each(bridgeDetection.nodes, function (i, bridgeDetectionNode) {

                        if (!bridgeDetectionNode.statusTree) {

                            bridgeDetectionNode.statusTree = new Tree({
                                "title": bridgeDetectionNode.title,
                                "class": bridgeDetectionNode.status.className
                            });

                            bridgeDetectionNode.typeTree = new Tree({
                                "title": bridgeDetectionNode.title,
                                "class": bridgeDetectionNode.type.className
                            });

                            bridgeDetectionNode.propertiesBlock = new BridgeDetectionNodeBlock();
                            bridgeDetectionNode.propertiesBlock.properties = bridgeDetectionNode;

                            bridgeDetectionNode.on("click", function () {
                                bridgeDetectionNode.bridgeDetection.activeNode(bridgeDetectionNode);
                                _this.app.pop.target = bridgeDetectionNode;
                                _this.app.pop.content = bridgeDetectionNode.propertiesBlock;
                                _this.app.pop.position = bridgeDetectionNode.position;
                                _this.app.pop.open();
                            });

                            bridgeDetectionNode.on("propertiesButtonClick", function () {
                                _this.app.pop.close();
                                _this.openNodeData(bridgeDetectionNode);
                            });

                            bridgeDetectionNode.statusTree.on("click", function () {
                                bridgeDetectionNode.bridgeDetection.activeNode(bridgeDetectionNode);
                            });

                            bridgeDetectionNode.typeTree.on("click", function () {
                                bridgeDetectionNode.bridgeDetection.activeNode(bridgeDetectionNode);
                            });

                        }

                        _this.nodeListPanel.treeBlock.tree.status.classNames[bridgeDetectionNode.status.className].add(bridgeDetectionNode.statusTree);
                        _this.nodeListPanel.treeBlock.tree.types.classNames[bridgeDetectionNode.type.className].add(bridgeDetectionNode.typeTree);

                    });


                    $.each(_this.nodeListPanel.treeBlock.tree.status.list, function (i, subTree) {
                        subTree.title += (" ( " + subTree.list.length + " )");
                        subTree.openChild();
                    });


                    $.each(_this.nodeListPanel.treeBlock.tree.types.list, function (i, subTree) {
                        subTree.title += (" ( " + subTree.list.length + " )");
                        subTree.openChild();
                    });

                    _this.nodeListPanel.bridgeDetection = bridgeDetection;

                    _this.nodeListPanel.open();

                });

            }
        },
        "openNodeData"          : {
            value: function (bridgeDetectionNode) {

                var _this = this;

                _this.chartPanel.open(false);
                _this.chartPanel.max(false);
                _this.chartPanel.chartBlock.title = bridgeDetectionNode.title;
                _this.chartPanel.tableBlock.clear();

                bridgeDetectionNode.loadData(function () {

                    var date = [];
                    var data = [];

                    $.each(bridgeDetectionNode.data, function (i, dataItem) {

                        //新旧系统数据临时区分
                        if (Number(bridgeDetectionNode["节点ID"]) > 1000) {

                            //旧系统
                            // {
                            //     "receive": 1697584804.3435104,
                            //     "ver": 1697584803,
                            //     "m_ts": 1697584804.3434799,
                            //     "receive_ts": 1697584804.343475,
                            //     "digital_angle": 1.6262505677731893,
                            //     "cali_angle": 1.6203819073139287,
                            //     "channel": "",
                            //     "chip_temp": 11.0803324099723,
                            //     "unix_time": 1697584801,
                            //     "main_monitor_data": 0.11603220251724024,
                            //     "raw_angle": 1.6090990754631866,
                            //     "project_name": "lxhtsljzdqdzdjc20210603",
                            //     "gateway_id": "FAB411192316998",
                            //     "packet_name": "IncliHCF2000V1",
                            //     "sensor_id": "1",
                            //     "trace": "pmq",
                            //     "microsecond": 0,
                            //     "data_type": "0",
                            //     "angle": 0.11603220251724024,
                            //     "module_name": "IncliHCF2000V1",
                            //     "node_id": "40293"
                            // }
                            var reverseItem = bridgeDetectionNode.data[bridgeDetectionNode.data.length - i - 1];
                            var reverseDataTime = new Date(reverseItem.dataObj.m_ts * 1000);

                            var dataTime = new Date(dataItem.dataObj.m_ts * 1000);
                            date.push(reverseDataTime.format("MM-dd hh:mm:ss"));
                            data.push(reverseItem.dataObj.main_monitor_data);

                            _this.chartPanel.tableBlock.add({
                                "data": dataItem.dataObj.main_monitor_data,
                                "time": dataTime.format("MM-dd hh:mm:ss")
                            });

                        } else {

                            //新系统
                            // {
                            //     "value": [
                            //     8533876
                            // ],
                            //     "node_id": "10"
                            // }

                            var reverseItem = bridgeDetectionNode.data[bridgeDetectionNode.data.length - i - 1];
                            var reverseDataTime = new Date(reverseItem.createTime);

                            var dataTime = new Date(dataItem.createTime);
                            date.push(reverseDataTime.format("MM-dd hh:mm:ss"));


                            var reverseData = reverseItem.dataObj.value;
                            var okData = dataItem.dataObj.value;

                            //数据格式区分
                            if (bridgeDetectionNode.typeCode === "0") {

                                //倾角
                                reverseData = reverseData[0];
                                okData = okData[0];

                            } else if (bridgeDetectionNode.typeCode === "1") {

                                //支座位移
                                reverseData /= 65536;
                                okData /= 65536;

                            } else if (bridgeDetectionNode.typeCode === "2") {

                                //支柱位移
                                reverseData /= 65536;
                                okData /= 65536;

                            }

                            data.push(reverseData);

                            _this.chartPanel.tableBlock.add({
                                "data": okData,
                                "time": dataTime.format("MM-dd hh:mm:ss")
                            });

                        }


                    });


                    _this.chartPanel.chartBlock.chart.setOption({
                        xAxis : {
                            data: date
                        },
                        series: [{
                            data: data
                        }]
                    });

                    _this.chartPanel.bridgeDetectionNode = bridgeDetectionNode;

                });

            }
        },
        "closeNode"             : {
            value: function (bridgeDetection) {

                var _this = this;

                _this.isOpened && _this.totalPanel.open(false);

                bridgeDetection.closeNode();
                _this.chartPanel.close(false);

                _this.app.flyHome();

                $.each(_this.items, function (i, item) {
                    item !== bridgeDetection && item.open();
                });

            }
        },
        "clear"                 : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"                  : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {

                    if (_this.app.main3DLayer) {
                        _this.sysMain3DLayerOpened = _this.app.main3DLayer.isOpened;
                        _this.app.ui.elements.tileSwitchPanel.tileSwitch.isChecked = false;
                    }

                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    _this.listPanel.open(false);
                    _this.totalPanel.open();

                    _this.isOpened = true;

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"              : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {

                        if (mapData) {
                            _this.objMap = mapData.obj;
                            _this.propertiesMap = mapData.properties;
                            _this.dic = mapData.dic;
                        }

                        _this.data = data;
                        _this.isLoaded = true;
                        callback && callback();


                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"             : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"                 : {
            value: function () {
                var _this = this;

                if (_this.sysMain3DLayerOpened) {
                    _this.app.ui.elements.tileSwitchPanel.tileSwitch.isChecked = true;
                }

                _this.chartPanel.close();
                _this.nodeListPanel.close();
                _this.totalPanel.open();

                $.each(_this.items, function (i, item) {
                    item.close();
                });

                _this.closePanel();

                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "closePanel"            : {
            value: function () {
                var _this = this;
                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        },
        "activeItem"            : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active(!item.isActive);
                item.listItem.toggleClass("active", item.isActive);
            }
        },
        "unActiveAllItem"       : {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        },
        "renderShCountBlockItem": {
            value: function (item) {
                var _this = this;

                var div = new mhjs.Div({"class": "item"});

                div.title = new mhjs.Div({"class": "title"});

                div.title.text(item.title);

                div.icon = new mhjs.Img({"class": "icon"});

                div.icon.attr("src", item.icon);

                div.count = new mhjs.Div({"class": "count"});

                div.count.text(item.count);

                div.count.unit = new mhjs.Span({"class": "unit"});

                div.count.unit.text(item.unit);

                div
                // .append(div.icon)
                    .append(div.title)
                    .append(div.count
                        .append(div.count.unit)
                    )

                ;

                return div;

            }
        }
    });

    return _class;

});